$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'pro/stock/list',
        datatype: "json",
        colModel: [			
			{ label: '初始数据ID', name: 'id_', width: 45, key: true,hidden:true},
			{ label: 'id', name: 'id', width: 45, key: true,hidden:true},
            { label: '设备类型', name: 'deviceType', width: 35, formatter:function(cellvalue, options, rowObject){
        		if(rowObject.deviceType=='pb'){
        			return  '平板';
        		}else if(rowObject.deviceType=='zj'){
        			return '支架';
        		}else if(rowObject.deviceType=='xsq'){
        			return '显示器';
        		}
        		
        	}},
            { label: '品牌', name: 'brand', width: 75},
            { label: '设备名称', name: 'deviceName', width: 75},
			{ label: '生产厂家', name: 'producer', width: 75},
			{ label: '物料编码', name: 'materialCode', width: 75 },
			{ label: '设备型号', name: 'typeNo', width: 55 },
/*			{ label: '出厂日期', name: 'shippingTime', width: 50},*/
			{ label: '尺寸(英寸)', name: 'facadeSize', width: 35},
			{ label: '规格', name: 'specification', width: 45},
			{ label: '检验人id', name: 'checkMan', width: 45,hidden:true},
			{ label: '检验人', name: 'username', width: 45},
			{ label: '备注', name: 'remarks', width: 120}
        ],
		viewrecords: true,
        
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    getSelectOpt("deviceType","DEVICE_TYPE",true);
});



var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			deviceName: null
		},
		showList: true,
		title:null,
		stock:{ 
		}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.stock = {deviceName:null, producer:null, typeNo:null,facadeSize:null};
		},
		update: function () {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
			vm.getPro(id);
		},
		del: function () {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "pro/stock/delete",
                    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
                                vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function () {
			var url = vm.stock.id_ == null ? "pro/stock/save" : "pro/stock/update";
			var arr =$("#deviceType").multipleSelect("getSelects");
			if(arr.length===0) {
				vm.stock.deviceType="";
			} else {
				vm.stock.deviceType=arr[0];
			}
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.stock),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getPro: function(proId){
			$.get(baseURL + "pro/stock/info/"+proId, function(r){
				vm.stock = r.stock;
				var arrs=[vm.stock.deviceType];
				$("#deviceType").multipleSelect("setSelects",arrs);
			});
		},
	 
		reload: function () {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'deviceName': vm.q.deviceName},
                page:page
            }).trigger("reloadGrid");
		}
	}
});